/*
 * MyTableModel.java
 *
 * Created on 2 de octubre de 2005, 04:04 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package ig.util;

//import com.ig.presentacion.util.comunes.Propiedades;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class ModelTabla_1 extends AbstractTableModel{
    private boolean     DEBUG=false;
    private boolean[]   editables=null;
    //private Object[][]  object;
    private String[] columnNames = null;
    private Object[][] data =null;
    private Object[][] data1 =null;
    private boolean delFisico=false;
    private JTable table=null;
    public ModelTabla_1(JTable tabla,Object[][] object,String[] columnNames){
        this.table=tabla;
        //this.editables=editables;
        
        this.columnNames=new String[columnNames.length+1];
        for (int i = 0; i < this.columnNames.length; i++) {
            if(columnNames.length>0)
                if(i==0)
                    this.columnNames[i]="C/F";
                else
                    this.columnNames[i]=columnNames[i-1];
        }
        this.editables=new boolean[columnNames.length+1];
        for (int i = 0; i < this.editables.length; i++) {
            if(i==0)this.editables[i]=false;
            else this.editables[i]=editables[i-1];
        }
        setData(object);
    }
    
    public void setColumnNames(String[] columnNames) {
        this.columnNames=new String[columnNames.length+1];
        for (int i = 0; i < this.columnNames.length; i++) {
            if(i==0)this.columnNames[i]="C/F";
            else this.columnNames[i]=columnNames[i-1];
        }
        this.editables=new boolean[columnNames.length+1];
        for (int i = 0; i < this.editables.length; i++) {
            if(i==0)this.editables[i]=false;
            else this.editables[i]=editables[i-1];
        }
    }
    
    public boolean isDelFisico() {
        return delFisico;
    }
    
    public void removeData(Object nameColumna){
        for (int i = 0; i < data.length; i++){
            if(data[i][0].toString().trim().equals(nameColumna.toString().trim())){
                deleteRC(i);
                break;
            }
        }
        table.setModel(new ModelTabla(table,data1,columnNames));
        this.fireTableDataChanged();
    }
    
    private void deleteRC(int pivote){
        String[] clonNames=columnNames.clone();
        columnNames=new String[clonNames.length-2];
        int k=0;
        for (int i = 1; i < clonNames.length; i++){
            if(i==pivote+1)
                i++;
            try {
                columnNames[k]=clonNames[i];
                k++;
            } catch (Exception e) {}
        }
        Object[][] clon=data1.clone();
        data1=new Object[data1.length-1][data1.length-1];
        k=0;
        int l=0;
        for (int i = 0; i < clon.length; i++) {
            l=0;
            for (int j = 0; j < clon.length; j++) {
                if(j==pivote)
                    j++;
                if(i==pivote)
                    i++;
                try {
                    data1[k][l]=clon[i][j];
                    l++;
                } catch (Exception e) {}
            }
            k++;
        }

    }
    
    public void addData(Object nameColumna){
        for (int i = 0; i < columnNames.length; i++){
            if(i<columnNames.length-1){
                columnNames[i]=columnNames[i+1];
            }else {
                columnNames[i]= nameColumna.toString().trim();
            }
            editables[i]=true;
        }
        table.setModel(new ModelTabla(table,data1,columnNames));
        this.fireTableDataChanged();
    }
    
    public void setData(Object[][] _data){
           if(_data!=null){
                data=new Object[_data.length][columnNames.length];
                for (int j = 0; j <columnNames.length; j++) {
                    for (int i = 0; i < _data.length; i++) {
                           this.data[i][j] = _data[i][j];
                    }
                }
           }
    }
    
    public int getColumnCount() {
        return columnNames.length;
    }
    
    public int getRowCount() {
        return data.length;
    }
    
    public String getColumnName(int col) {
        return columnNames[col];
    }
    
    public Object getValueAt(int row, int col) {
        return data[row][col];
    }
    
    public Class getColumnClass(int c) {
        return String.class;
    }
    
    public boolean isCellEditable(int row, int col) {
        if (editables[col]==true && row!=col-1)
            return true;
        else
            return false;
    }
    
    public Object[][] getData() {
        return this.data1;
    }
    
    public void setValueAt(Object value, int row, int col){
        if (DEBUG) {
            System.out.println("Setting value at " + row + "," + col
                    + " to " + value
                    + " (an instance of "
                    + value.getClass() + ")");
        }
        data[row][col] = value;
        data[col-1][row+1] = value;
        
        data1[row][col-1] = value;
        data1[col-1][row] = value;
        this.fireTableCellUpdated(row,col);
        this.fireTableCellUpdated(col-1,row+1);
    }
    
}
